package com.bestcem.xm.award.dao.impl.mysql;

import com.bestcem.xm.award.dao.AwardWinDao;
import com.bestcem.xm.award.dao.mapper.AwardWinMapper;
import com.bestcem.xm.award.entity.mysql.AwardWinEntity;
import com.bestcem.xm.award.entity.pojo.AwardWinDO;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.common.core.uitls.StringUtil;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * ds_award_win dao-mysql实现
 *
 * @author hang.li <hang.li@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/18 17:02
 */
@Repository("winDaoMysql")
public class AwardWinDaoImpl implements AwardWinDao {

    @Resource
    private AwardWinMapper winMapper;

    @Override
    public AwardWinDO findById(String id) {
        return awardWinEntityToDo(winMapper.findById(id));
    }

    @Override
    public long updateUsedByAwardId(String awardId, Boolean used) {
        return winMapper.updateUsedByAwardId(awardId, used, DateUtil.getCommonDate());
    }

    @Override
    public long deleteByAwardId(String awardId) {
        return winMapper.deleteByAwardId(awardId);
    }

    @Override
    public long insertMany(List<AwardWinDO> awardWins) {
        if (CollectionUtils.isEmpty(awardWins)) {
            return 0;
        }
        List<AwardWinEntity> winEntityList = new ArrayList<>();
        for (AwardWinDO awardWin : awardWins) {
            AwardWinEntity winEntity = new AwardWinEntity();
            winEntity.setWinId(StringUtil.getId());
            winEntity.setAwardId(awardWin.getAwardId())
                    .setCode(awardWin.getCode())
                    .setLevel(awardWin.getLevel())
                    .setName(awardWin.getName())
                    .setUsed(awardWin.getUsed())
                    .setCreateTime(DateUtil.getCommonDate())
                    .setUpdateTime(DateUtil.getCommonDate());
            winEntityList.add(winEntity);
        }
        return winMapper.insertMany(winEntityList);
    }

    @Override
    public List<AwardWinDO> listByAwardId(String awardId, String[] includeFields, String[] excludeFields) {
        List<AwardWinEntity> winEntityList = winMapper.findListByAwardId(awardId);
        List<AwardWinDO> winDOList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(winEntityList)) {
            for (AwardWinEntity winEntity : winEntityList) {
                winDOList.add(awardWinEntityToDo(winEntity));
            }
        }
        return winDOList;
    }

    @Override
    public AwardWinDO findAndUpdateByIdAndNameAndUsed(String awardId, String name, Boolean used) {
        AwardWinEntity winEntity = winMapper.findWinByIdAndNameAndUsed(awardId, name, used);
        // 标记使用状态
        if (Objects.nonNull(winEntity)) {
            winMapper.updateUsedByAwardWinId(winEntity.getWinId(), !used, DateUtil.getCommonDate());
        }
        return awardWinEntityToDo(winEntity);
    }

    /**
     * AwardWin数据转换
     *
     * @param winEntity AwardWinEntity
     * @return AwardWinDO
     */
    private AwardWinDO awardWinEntityToDo(AwardWinEntity winEntity) {
        if (Objects.nonNull(winEntity)) {
            AwardWinDO winDO = new AwardWinDO();
            winDO.setId(winEntity.getWinId());
            winDO.setAwardId(winEntity.getAwardId());
            winDO.setCode(winEntity.getCode());
            winDO.setLevel(winEntity.getLevel());
            winDO.setUsed(winEntity.getUsed());
            winDO.setName(winEntity.getName());
            winDO.setCreatedDate(winEntity.getCreateTime());
            winDO.setUpdatedDate(winEntity.getUpdateTime());
            return winDO;
        }
        return null;
    }
}